const app = getApp()
var common = require('../../../static/utils/common.util.js');
var tagStyle00 = "background: #efefef;\n  color: #555;\n  padding: 0 5px;\n  border-radius: 10px;\n  text-align: center;\n  height: 25px;\n  display: flex;\n  justify-content: center;\n  align-items: center;\n  line-height: 20px;\n";

var selectStyle = "background: #54d09f;\n  color: #fff;\n  padding: 0 5px;\n  border-radius: 10px;\n  text-align: center;\n  height: 25px;\n  display: flex;\n  justify-content: center;\n  align-items: center;\n  line-height: 20px;\n";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zpxxId:null,
    avatarUrl: "",
    assoName: "",
    collegeName: "",
    assoType: "",
    startTime: "",
    studentName: "",
    stjj: "",
    stxq: null,
    stbm: [],
    stzps: [],
    swiperCurrent: 0,
    depList: [],
    depDesc: "",
    isFollow:null,
    isDelivery:false,
    isCheck:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if(options.isCheck){
      console.log("==========")
      that.setData({
        isCheck:options.isCheck
      })
    }
    wx.request({
      url: app.globalData.url+'/zpxx/getZpxxDetail',
      data:{
        zpxxId:options.zpxxId
      },
      success:res=>{
        console.log(res)
        let{
          assoName,
          assoType,
          avatarUrl,
          collegeName,
          startTime,
          depImgs,
          depList,
          schoolName,
          stjj,
          stxq,
          studentName
        } = res.data.zpxxVO
        that.setData({
          zpxxId:options.zpxxId,
          assoName:assoName,
          assoType: assoType,
          avatarUrl: avatarUrl,
          collegeName: collegeName,
          startTime: startTime,
          stzps: JSON.parse(depImgs),
          schoolName: schoolName,
          stjj: stjj,
          stxq: stxq,
          studentName: studentName
        })
        var lists = []
        if (JSON.parse(depList)!=null){
          JSON.parse(depList).forEach(function (item, index) {
            lists.push({
              text: item.depName,
              depDesc: item.depDesc,
              tagStyle: tagStyle00,
              tagSelectedStyle: selectStyle,
              checked: false
            })
          })
        }
        if (lists.length != 0) {
          lists[0].checked = true
          that.setData({
            depList: lists,
            depDesc: lists[0].depDesc
          })
        }
        wx.request({
          url: app.globalData.url+'/zpxx/isFollow',
          data:{
            uniqueId:app.globalData.uniqueId,
            zpxxId:that.data.zpxxId
          },
          success:res=>{
            console.log(res.data)
            if(res.data){
              that.setData({
                isFollow:true
              })
            }else{
              that.setData({
                isFollow:false
              })
            }
          }
        })
        wx.request({
          url: app.globalData.url+'/zpxx/isDelivery',
          data:{
            uniqueId:app.globalData.uniqueId,
            zpxxId:this.data.zpxxId
          },
          success:res=>{
            if(res.data==true){
              that.setData({
                isDelivery : true
              })
            }
          }
        })
      }
    })
  },
  follow:function(){
    var that = this
    wx.request({
      url: app.globalData.url+'/zpxx/follow',
      data:{
        uniqueId:app.globalData.uniqueId,
        zpxxId:this.data.zpxxId
      },
      success:res=>{
        console.log(res.data.code)
        if(res.data.code==1){
          wx.showToast({
            title: '收藏成功',
          })
          that.setData({
            isFollow: true
          })
        }else{
          wx.showToast({
            title: '收藏失败',
          })
        }
      }
    })
  },
  unfollow:function(){
    var that = this
    wx.request({
      url: app.globalData.url+'/zpxx/unfollow',
      data:{
        uniqueId:app.globalData.uniqueId,
        zpxxId:this.data.zpxxId
      },
      success:res=>{
        console.log(res.data.code)
        if(res.data.code==1){
          wx.showToast({
            title: '取消收藏',
          })
          that.setData({
            isFollow: false
          })
        } else {
          wx.showToast({
            title: '取消收藏失败',
          })
        }
      }
    })
  },
  singleTap: function singleTap(e) {
    var opt = e.detail.index;
    this.data.depList.forEach(function (item, index) {
      item.checked = index === opt;
    });
    this.setData({
      depList: this.data.depList,
      depDesc: this.data.depList[opt].depDesc
    });
  },
  previewImage: function () {
    wx.previewImage({
      current: this.data.stzps[this.data.swiperCurrent], // 当前显示图片的http链接
      urls: this.data.stzps // 需要预览的图片http链接列表
    })
    wx.cloud.getTempFileURL({
      fileList: this.data.stzps,
      success: res => {
        // fileList 是一个有如下结构的对象数组
        // [{
        //    fileID: 'cloud://xxx.png', // 文件 ID
        //    tempFileURL: '', // 临时文件网络链接
        //    maxAge: 120 * 60 * 1000, // 有效期
        // }]
        console.log(res.fileList)
      },
      fail: console.error
    })
  },
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  sendButton: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: "确认提交",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.delivery();
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },
  delivery:function(){
    var that = this
    console.log(this.data.depList)
    console.log(this.data.zpxxId)
    wx.navigateTo({
      url: '../delivery/delivery?depList=' + JSON.stringify(that.data.depList) 
      +"&zpxxId="+that.data.zpxxId,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})